use http::{header, HttpTryFrom};
use actix_web::{HttpRequest, HttpResponse, Error};
use actix_web::middleware::{Middleware, Started, Response};

pub struct SongMiddleware;

impl<S> Middleware<S> for SongMiddleware {
    fn start(&self, req: &HttpRequest<S>) -> Result<Started, Error> {
        println!("Hello request: {:?}", req);
        Ok(Started::Done)
    }

    fn response(&self, req: &HttpRequest<S>, mut resp: HttpResponse) -> Result<Response, Error> {
        println!("Hello response: {:?}", resp);
        resp.headers_mut().insert(
            header::HeaderName::try_from("X-VERSION").unwrap(),
            header::HeaderValue::from_static("0.2"));
        Ok(Response::Done(resp))
    }
}